package com.whfc.ms.api.mach.service.impl;

import com.github.pagehelper.PageInfo;
import com.whfc.common.enums.DeviceType;
import com.whfc.common.result.PageData;
import com.whfc.common.util.DateUtil;
import com.whfc.common.util.PageUtil;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.fuum.service.SysDeptService;
import com.whfc.mach.dto.AppMachDataStatDTO;
import com.whfc.mach.dto.MachDayDataDTO;
import com.whfc.mach.dto.MachMonthReportDTO;
import com.whfc.mach.service.AppReportService;
import com.whfc.ms.api.mach.service.MsReportService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author sun_guodong
 * @description
 * @date 2021-03-10
 */
@Service
public class MsReportServiceImpl implements MsReportService {
    @DubboReference(interfaceClass = AppReportService.class, version = "1.0.0")
    private AppReportService appReportService;

    @DubboReference(interfaceClass = SysDeptService.class, version = "1.0.0")
    private SysDeptService sysDeptService;

    @Override
    public PageData<MachDayDataDTO> dayReport(Integer deptId, Date date, Integer pageNum, Integer pageSize) {
        List<Integer> deptIds = sysDeptService.getDescendantDeptIdList(deptId);
        if (deptIds == null || deptIds.size() == 0) {
            return PageUtil.pageData(PageInfo.of(Collections.EMPTY_LIST));
        }
        return appReportService.dayReport(deptIds, date, pageNum, pageSize);
    }

    @Override
    public PageData<MachMonthReportDTO> monthReport(Integer deptId, Integer pageNum, Integer pageSize, Date month) {
        List<Integer> deptIds = sysDeptService.getDescendantDeptIdList(deptId);
        if (deptIds == null || deptIds.size() == 0) {
            return PageUtil.pageData(PageInfo.of(Collections.EMPTY_LIST));
        }
        return appReportService.monthReport(deptIds, pageNum, pageSize, DateUtil.getMonthBegin(month), DateUtil.getMonthEnd(month));
    }

    @Override
    public PageData<MachMonthReportDTO> commonReport(Integer deptId, Integer pageNum, Integer pageSize, Date startDate, Date endDate) {
        List<Integer> deptIds = sysDeptService.getDescendantDeptIdList(deptId);
        if (deptIds == null || deptIds.size() == 0) {
            return PageUtil.pageData(PageInfo.of(Collections.EMPTY_LIST));
        }
        return appReportService.monthReport(deptIds, pageNum, pageSize, startDate, endDate);
    }

    @Override
    public PageData<MachMonthReportDTO> yearReport(Integer deptId, Integer pageNum, Integer pageSize, Integer year) {
        List<Integer> deptIds = sysDeptService.getDescendantDeptIdList(deptId);
        if (deptIds == null || deptIds.size() == 0) {
            return PageUtil.pageData(PageInfo.of(Collections.EMPTY_LIST));
        }
        return appReportService.yearReport(deptIds, pageNum, pageSize, year);
    }

    @Override
    public AppMachDataStatDTO dayReportStat(Integer deptId, Date date) {
        List<Integer> deptIds = sysDeptService.getDescendantDeptIdList(deptId);
        if (deptIds == null || deptIds.size() == 0) {
            return new AppMachDataStatDTO();
        }
        return appReportService.reportStat(deptIds, date, date);
    }

    @Override
    public AppMachDataStatDTO monthReportStat(Integer deptId, Date month) {
        List<Integer> deptIds = sysDeptService.getDescendantDeptIdList(deptId);
        if (deptIds == null || deptIds.size() == 0) {
            return new AppMachDataStatDTO();
        }
        return appReportService.reportStat(deptIds, DateUtil.getMonthBegin(month), DateUtil.getMonthEnd(month));
    }

    @Override
    public AppMachDataStatDTO reportStat(Integer deptId, Date startDate, Date endDate) {
        List<Integer> deptIds = sysDeptService.getDescendantDeptIdList(deptId);
        if (deptIds == null || deptIds.size() == 0) {
            return new AppMachDataStatDTO();
        }
        return appReportService.reportStat(deptIds, startDate, endDate);
    }

    @Override
    public AppMachDataStatDTO yearReportStat(Integer deptId, Integer year) {
        List<Integer> deptIds = sysDeptService.getDescendantDeptIdList(deptId);
        if (deptIds == null || deptIds.size() == 0) {
            return new AppMachDataStatDTO();
        }
        return appReportService.yearReportStat(deptIds, year);
    }

    @Override
    public OssPathDTO dayReportExport(Integer deptId, Date date) {
        String path = appReportService.dayReportExport(deptId, date, DeviceType.obd.getValue());
        return new OssPathDTO(path);
    }

    @Override
    public OssPathDTO monthReportExport(Integer deptId, Date month) {
        Date startDate = DateUtil.getMonthBegin(month);
        Date endDate = DateUtil.getMonthEnd(month);
        String path = appReportService.monthReportExport(deptId, startDate, endDate, DeviceType.obd.getValue());
        return new OssPathDTO(path);
    }

    @Override
    public OssPathDTO export(Integer deptId, Date startDate, Date endDate) {
        String path = appReportService.monthReportExport(deptId, startDate, endDate, DeviceType.obd.getValue());
        return new OssPathDTO(path);
    }

    @Override
    public OssPathDTO yearReportExport(Integer deptId, Integer year) {
        String path = appReportService.yearReportExport(deptId, year);
        return new OssPathDTO(path);
    }

    @Override
    public OssPathDTO commonDayReportExport(Integer deptId, Date date) {
        String path = appReportService.dayReportExport(deptId, date, DeviceType.mach.getValue());
        return new OssPathDTO(path);
    }

    @Override
    public OssPathDTO commonMonthReportExport(Integer deptId, Date month) {
        Date startDate = DateUtil.getMonthBegin(month);
        Date endDate = DateUtil.getMonthEnd(month);
        String path = appReportService.monthReportExport(deptId, startDate, endDate, DeviceType.mach.getValue());
        return new OssPathDTO(path);
    }

    @Override
    public OssPathDTO commonExport(Integer deptId, Date startDate, Date endDate) {
        String path = appReportService.monthReportExport(deptId, startDate, endDate, DeviceType.mach.getValue());
        return new OssPathDTO(path);
    }
}
